#include"cmdStack.h"
//init  ..
//查表后填写
int initStack(taskStack_t *taskStack, char *path) {
    bzero(taskStack, sizeof(taskStack_t));
    taskStack->size = 0;
    node_t *pNew = (node_t *)calloc(1, sizeof(node_t));
    memcpy(pNew->data, path, strlen(path));
    pNew->pNext = NULL;
    taskStack->pFront = pNew;
    //存根
    taskStack->pTail = pNew;
    taskStack->size++;
    return 0;
}
//enStack
int enStack(taskStack_t *taskStack, char *path) {
    node_t *pNew = (node_t *)calloc(1, sizeof(node_t));
    //memcpy(&pNew->data, path, strlen(path));
    strcpy(pNew->data,path);
    //printf("enStack path = %s\n",path);
    //printf("Stack size = %d\n",taskStack->size);
    if(taskStack->size == 1)
    {
        pNew->pNext = taskStack->pFront;
        pNew->pNext = taskStack->pTail; //tail 往前移
        taskStack->pFront = pNew;   
        taskStack->pTail = pNew;
        //printf("data = %s\n",taskStack->pFront->data);
    }
    pNew->pNext = taskStack->pFront;
    taskStack->pFront = pNew;
    //printf("data = %s\n",taskStack->pFront->data);
    ++taskStack->size;
    return 0;
}
//deStack
int deStack(taskStack_t *taskStack) {
    node_t *pCur = taskStack->pFront;
    if(taskStack->size > 1) {
        taskStack->pFront = pCur->pNext;
        free(pCur);
        --taskStack->size;
    }
    return 0;
}
int visitStack(taskStack_t *taskStack, char *path) {
    char buf[1024];
    node_t *pCur = taskStack->pFront;

    strcpy(path, ""); // 初始化path为空字符串

    while (pCur) {
        strcpy(buf, "/");
        strcat(buf, pCur->data);
        strcat(buf, path);
        strcpy(path, buf);
        pCur = pCur->pNext;
    }

    // 去除末尾多余的斜杠
    if (strlen(path) > 0 && path[strlen(path) - 1] == '/') {
        path[strlen(path) - 1] = '\0';
    }

    return 0;
}